package com.glasgowrax.hack.domain.directions;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.gms.maps.model.LatLng;

public class Leg {
	
	private long arrivalTimeSeconds;
	private String arrivalTimeDescription;
	
	private long departureTimeSeconds;
	private String departureTimeDescription;
	
	private double distanceMeters;
	private String distanceDescription;
	
	private long durationSeconds;
	private String durationDescription;
	
	private String endAddress;
	private LatLng endPoint;
	
	private String startAddress;
	private LatLng startPoint;
	
	private List<Step> steps;
	
	public Leg(JSONObject json) {
		
		try {
			JSONObject arrivalTime = (json.has("arrival_time")) ? json.getJSONObject("arrival_time") : new JSONObject();
			JSONObject departureTime = (json.has("departure_time")) ? json.getJSONObject("departure_time") : new JSONObject();
			JSONObject distance = (json.has("distance")) ? json.getJSONObject("distance") : new JSONObject();
			JSONObject duration = (json.has("duration")) ? json.getJSONObject("duration") : new JSONObject();
			JSONObject end = (json.has("end_location")) ? json.getJSONObject("end_location") : new JSONObject();
			JSONObject start = (json.has("start_location")) ? json.getJSONObject("start_location") : new JSONObject();
			JSONArray stepList = (json.has("steps")) ? json.getJSONArray("steps") : new JSONArray();
			
			arrivalTimeSeconds = (arrivalTime.has("value")) ? arrivalTime.getLong("value") : -1;
			arrivalTimeDescription = (arrivalTime.has("text")) ? (String) arrivalTime.get("text") : "";
			
			departureTimeSeconds = (departureTime.has("value")) ? departureTime.getLong("value") : -1;
			departureTimeDescription = (departureTime.has("text")) ? (String) departureTime.get("text") : "";
			
			distanceMeters = (distance.has("value")) ? distance.getLong("value") : -1;
			distanceDescription = (distance.has("text")) ? (String) distance.get("text") : "";

			durationSeconds = (duration.has("value")) ? duration.getLong("value") : -1;
			durationDescription = (duration.has("text")) ? (String) duration.get("text") : "";
			
			endAddress = (json.has("end_address")) ? (String) json.get("end_address") : "";
			startAddress = (json.has("start_address")) ? (String) json.get("start_address") : "";
			
			endPoint = (end.has("lat") && end.has("lng")) ? new LatLng(end.getDouble("lat"), end.getDouble("lng")) : null;
			startPoint = (start.has("lat") && start.has("lng")) ? new LatLng(start.getDouble("lat"), start.getDouble("lng")) : null;
			
			steps = new ArrayList<Step>();
			for (int i=0; i<stepList.length(); i++) {
				steps.add(new Step(stepList.getJSONObject(i)));
			}
		
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	public long getArrivalTimeSeconds() {
		return arrivalTimeSeconds;
	}

	public void setArrivalTimeSeconds(long arrivalTimeSeconds) {
		this.arrivalTimeSeconds = arrivalTimeSeconds;
	}

	public String getArrivalTimeDescription() {
		return arrivalTimeDescription;
	}

	public void setArrivalTimeDescription(String arrivalTimeDescription) {
		this.arrivalTimeDescription = arrivalTimeDescription;
	}

	public long getDepartureTimeSeconds() {
		return departureTimeSeconds;
	}

	public void setDepartureTimeSeconds(long departureTimeSeconds) {
		this.departureTimeSeconds = departureTimeSeconds;
	}

	public String getDepartureTimeDescription() {
		return departureTimeDescription;
	}

	public void setDepartureTimeDescription(String departureTimeDescription) {
		this.departureTimeDescription = departureTimeDescription;
	}

	public double getDistanceMeters() {
		return distanceMeters;
	}

	public void setDistanceMeters(double distanceMeters) {
		this.distanceMeters = distanceMeters;
	}

	public String getDistanceDescription() {
		return distanceDescription;
	}

	public void setDistanceDescription(String distanceDescription) {
		this.distanceDescription = distanceDescription;
	}

	public long getDurationSeconds() {
		return durationSeconds;
	}

	public void setDurationSeconds(long durationSeconds) {
		this.durationSeconds = durationSeconds;
	}

	public String getDurationDescription() {
		return durationDescription;
	}

	public void setDurationDescription(String durationDescription) {
		this.durationDescription = durationDescription;
	}

	public String getEndAddress() {
		return endAddress;
	}

	public void setEndAddress(String endAddress) {
		this.endAddress = endAddress;
	}

	public LatLng getEndPoint() {
		return endPoint;
	}

	public void setEndPoint(LatLng endPoint) {
		this.endPoint = endPoint;
	}

	public String getStartAddress() {
		return startAddress;
	}

	public void setStartAddress(String startAddress) {
		this.startAddress = startAddress;
	}

	public LatLng getStartPoint() {
		return startPoint;
	}

	public void setStartPoint(LatLng startPoint) {
		this.startPoint = startPoint;
	}

	public List<Step> getSteps() {
		return steps;
	}

	public void setSteps(List<Step> steps) {
		this.steps = steps;
	}

	public boolean isPartlyTransit() {
		for (Step s : steps) {
			if (s.getTransitDetails() != null) return true;
		}
		return false;
	}
	
	
	
}
